﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PaliTextReader2.Utilities;

namespace PaliTextReader2
{
    public partial class AddTermDlg : Form
    {
        int m_nEntryID;

        public AddTermDlg()
        {
            InitializeComponent();
        }

        public AddTermDlg(int TermID)
        {
            InitializeComponent();
            m_nEntryID = TermID;
        }

        private void AddTermDlg_Load(object sender, EventArgs e)
        {
            if (m_nEntryID > 0)
            {
                ContentsDS.DictionaryContentDataTable objPEDDTable = ctlDictionaryContentTA.GetTranslation(m_nEntryID);
                ctlPaliTerm.Text = objPEDDTable[0].Keyword;
                ctlTranslation.Text = objPEDDTable[0].Translation;
            }
        }

        private void ctlOK_Click(object sender, EventArgs e)
        {
            if (ValidateChildren())
            {
                if (m_nEntryID > 0)
                    ctlDictionaryContentTA.Update(ctlPaliTerm.Text, ctlTranslation.Text,
                        Utility.ConvertDiac(ctlPaliTerm.Text), Int32.Parse(ctlDictionary.Text),
                        m_nEntryID);
                else
                    ctlDictionaryContentTA.Insert(ctlPaliTerm.Text, ctlTranslation.Text,
                        Utility.ConvertDiac(ctlPaliTerm.Text), Int32.Parse(ctlDictionary.Text));
                
                DialogResult = DialogResult.OK;
            }
        }

        private void ctlPaliTerm_KeyDown(object sender, KeyEventArgs e)
        {
            PaliKeyboard.HandlePaliKeys(ctlPaliTerm, e);
        }

        private void ctlTranslation_KeyDown(object sender, KeyEventArgs e)
        {
            PaliKeyboard.HandlePaliKeys(ctlTranslation, e);
        }

        private void ctlPaliTerm_Validating(object sender, CancelEventArgs e)
        {
            if (ctlPaliTerm.Text.Trim() == String.Empty)
            {
                ctlError.SetError(ctlPaliTerm, "term required");
                e.Cancel = true;
            }
            else
                ctlError.SetError(ctlPaliTerm, "");
        }

        private void ctlTranslation_Validating(object sender, CancelEventArgs e)
        {
            if (ctlTranslation.Text.Trim() == String.Empty)
            {
                ctlError.SetError(ctlTranslation, "translation required");
                e.Cancel = true;
            }
            else
                ctlError.SetError(ctlTranslation, "");
        }
    }
}
